//
//  VideoCollectionViewCell.m
//  项目一
//
//  Created by 王鑫 on 15/11/13.
//  Copyright (c) 2015年 王鑫. All rights reserved.
//

#import "VideoCollectionViewCell.h"
#import "VideoTableViewCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "VideoDetailViewController.h"
#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height

@implementation VideoCollectionViewCell
#pragma mark - Method dealloc
- (void)dealloc {
    [_videoTableView release];
    [_videoArray release];
    [super dealloc];
}
#pragma mark - Method initWithFrame
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createTableView];
    }
    return self;
}
#pragma mark - Method createTableView
- (void)createTableView {
    self.videoTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) style:UITableViewStylePlain];
    [self.contentView addSubview:self.videoTableView];
    self.videoTableView.dataSource = self;
    self.videoTableView.delegate = self;
    [self createData];
    [_videoTableView release];
}
#pragma mark - Method heightForRowAtIndexPath
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}
#pragma mark - Method number Of Rows In Section
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.videoArray.count;
}
#pragma mark - Method
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *videoReuse = @"videoReuse";
    VideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:videoReuse];
    if (!cell) {
        cell = [[VideoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:videoReuse];
    }
    VideoModel *videoModel = self.videoArray[indexPath.row];
    cell.model = videoModel;
    return cell;
}
#pragma mark - Method layoutSubviews
- (void)layoutSubviews {
    [super layoutSubviews];
    self.videoTableView.frame = CGRectMake(0, 0, WIDTH, HEIGHT);
}


#pragma mark - Method createData
- (void)createData {
    self.videoArray = [NSMutableArray array];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://app.api.autohome.com.cn/autov5.0.0/news/videolist-pm1-vt0-s30-lastid0.json" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSMutableArray *listArray = dic[@"result"][@"list"];
        for (NSDictionary *tempDic in listArray) {
            VideoModel *model = [[VideoModel alloc] init];
            [model setValuesForKeysWithDictionary:tempDic];
            [self.videoArray addObject:model];
            [model release];
        }
        [self.videoTableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@", error);
    }];
}
#pragma mark - Method did Select Row At IndexPath
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate VideoPushToWebView:self.videoArray[indexPath.row]];
}
@end
